#include "UploadPictures.h"
#include "ImageCell.h"
#include <QFileDialog>
namespace UserManagementSystem {

UploadPictures::UploadPictures(QWidget *parent)
    : QWidget{parent}
{
    this->setMinimumSize(m_itemWidth+5,m_itemWidth+5);

    m_mainLayout=new QVBoxLayout(this);

    m_listWidget=new QListWidget(this);
    m_listWidget->setViewMode(QListView::IconMode);
    m_listWidget->setFlow(QListView::LeftToRight);
    m_listWidget->setResizeMode(QListWidget::Adjust);
    m_listWidget->setWrapping(true);
    m_listWidget->setGridSize(QSize(m_itemWidth,m_itemWidth));//item的大小
    m_listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_listWidget->setStyleSheet(R"(
        QListWidget::item{
            border-radius:10px;
        }
        QListWidget::item:hover {
            background-color: lightblue;
            border-radius:10px;
        }
     )");

    //将tablewidget添加在布局中
    m_mainLayout->addWidget(m_listWidget);
    m_mainLayout->setContentsMargins(0,0,0,0);
    this->setStyleSheet("border:none;");
    //添加初始化的按钮
    AddInitCell();
}

void UploadPictures::AddInitCell()
{
    ImageCell* add_cell=new ImageCell(nullptr,false,"");

    m_addImage=new QListWidgetItem();

    //绑定信号槽
    QObject::connect(add_cell,&ImageCell::On_Add_Image,this,&UploadPictures::AddImage);

    m_addImage->setSizeHint(QSize(m_itemWidth,m_itemWidth));

    m_listWidget->addItem(m_addImage);
    m_listWidget->setItemWidget(m_addImage,add_cell);

}

void UploadPictures::AddImage()
{
    //打开一个文件对话框
    QString file_path=QFileDialog::getOpenFileName(this,"选择图片","","Images(*.png *.jpg *.jpeg *.bmp)");

    if(file_path.isEmpty()){
        return ;
    }
    ImageCell* new_cell=new ImageCell(nullptr,true,file_path);
    connect(new_cell,&ImageCell::On_Delete_Image,this,&UploadPictures::DeleteImage);

    //新建一个item,将item和new_cell连接到一起,插入listwidget
    QListWidgetItem * new_item=new QListWidgetItem();
    new_item->setSizeHint(QSize(m_itemWidth,m_itemWidth));

    //插入最左侧
    m_listWidget->insertItem(0,new_item);
    m_listWidget->setItemWidget(new_item,new_cell);

    //新增之后检查item个数
    int item_cnt=m_listWidget->count();
    int row=(item_cnt-1)/m_col;
    qDebug()<<"item_cnt:"<<item_cnt<<"row:"<<row;
    qDebug()<<"width:"<<this->rect().width();
    if(row>=1){
        qDebug()<<"old_h:"<<this->height();

        this->setFixedHeight((row+1)*m_itemWidth+10);
        qDebug()<<"new_h:"<<this->height();
    }
}

void UploadPictures::DeleteImage()
{
    //删除图片
    qDebug()<<"删除图片";

    //获得发送信号的窗口
    ImageCell* senderCell = qobject_cast<ImageCell*>(sender());
    if (!senderCell) return;

    // 查找对应 item
    for (int i = 0; i < m_listWidget->count(); i++) {
        QListWidgetItem* item = m_listWidget->item(i);
        if (m_listWidget->itemWidget(item) == senderCell) {
            QListWidgetItem* removedItem = m_listWidget->takeItem(i);
            delete removedItem;
            break;
        }
    }

    //新增之后检查item个数
    int item_cnt=m_listWidget->count();
    int row=(item_cnt-1)/m_col;
    qDebug()<<"item_cnt:"<<item_cnt<<"row:"<<row;

    if(row>=0){
        qDebug()<<"old_h:"<<this->height();
        qDebug()<<"old_width:"<<this->rect().width();
        this->setFixedHeight((row+1)*m_itemWidth+10);
        qDebug()<<"new_h:"<<this->height();
    }
}
}

